package com.linc.concurrency.preload;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class ProductInfo {
  public static ProductInfo loadProductInfo() {
    return null;
  }
}

public class Preloader {
  private final FutureTask<ProductInfo> futureTask =
      new FutureTask<ProductInfo>(ProductInfo::loadProductInfo);
  private final Thread thread = new Thread(futureTask);

  public void start() {
    thread.start();
  }

  public ProductInfo get() throws InterruptedException {
    try {
      return futureTask.get();
    } catch (ExecutionException e) {
      Throwable course = e.getCause();
      throw launderThrowable(course);
    }
  }

  public static RuntimeException launderThrowable(Throwable t) {
    if (t instanceof RuntimeException) return (RuntimeException) t;
    else if (t instanceof Error) throw (Error) t;
    else throw new IllegalStateException("Not checked", t);
  }
}
